package com.issac.service.serviceImpl;

import com.issac.common.R;
import com.issac.dto.ContractDto;
import com.issac.dto.FruitDto;
import com.issac.dto.MiddleTabDto;
import com.issac.entity.Contract;
import com.issac.entity.Fruit;
import com.issac.mapper.ContractMapper;
import com.issac.mapper.FruitMapper;
import com.issac.service.ContractService;
import com.issac.vo.ContractVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ContractServiceImpl
 * @Author zy
 * @Date 2022/6/23 9:19
 * @Description ContractServiceImpl
 * @Version 1.0
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired ContractMapper contractMapper;
    @Autowired FruitMapper fruitMapper;
    @Override
    public R createContract(Contract contract) {
        contractMapper.createContract(contract);

        List middleList = getMiddleDtoByContract(contract);
        contractMapper.insertMiddleTab(middleList);

        return R.ok();
    }

    @Override
    public R updateContract(Contract contract) {
        ContractDto contractDto = new ContractDto();
        contractDto.setType(contract.getType());
        contractDto.setRetailerId(contract.getRetailer().getRetailerId());
        contractDto.setContractId(contract.getContractId());
        // 1.更新合同表
        contractMapper.updateContract(contractDto);

        // 2.删除中间表再重新插入新数据
        contractMapper.deleteMidTabByCId(contract.getContractId());
        List middleList = getMiddleDtoByContract(contract);
        contractMapper.insertMiddleTab(middleList);

        // 3.修改水果信息
        List<FruitDto> fruitList = contract.getFruitList();
        fruitMapper.updateList(fruitList);
        return R.ok();
    }

    /**
     * 删除合同，同时需要删除中间表
     * @param contractId 合同id
     * @return
     */
    @Override
    public R deleteContract(String contractId) {
        contractMapper.deleteContractByCId(contractId);
        contractMapper.deleteMidTabByCId(contractId);

        return R.ok();
    }

    @Override
    public R queryAllContract() {
        List<ContractVo> contractList = contractMapper.queryAllContract();
        return R.ok(contractList);
    }

    @Override
    public R queryContract(String id) {
        Contract contract = contractMapper.queryContract(id);
        return R.ok(contract);
    }

    /**
     * 通过购销合同获取应存入中间表的数据集合
     * @param contract 购销合同
     * @return 水果集合
     */
    public List getMiddleDtoByContract(Contract contract){
        ArrayList<MiddleTabDto> middleList = new ArrayList<>();
        // 一对多，每一个购销合同中都包含多种商品
        for (FruitDto fruit : contract.getFruitList()) {
            MiddleTabDto middleTabDto = new MiddleTabDto();
            middleTabDto.setContractId(contract.getContractId());
            middleTabDto.setFruitId(fruit.getFruitId());
            middleTabDto.setNumber(fruit.getNum());
            middleList.add(middleTabDto);
        }
        return middleList;
    }
}
